import { getBaseUrl } from '@/services/urlconst';
import { getUserID, handleCheckSearchParams, requestFunComponFiex } from '@/utils';
import React, { useMemo } from 'react';
import { useRequest } from 'umi';
import { ManageCommonAPI } from '@/pages/type';
import { ManageTabSwitch, getSearchList, taskSearchParams } from '../mconfig';

const baseUrl = getBaseUrl();

/**
 * 管理页面处理数据
 *
 * 【启用】
 * */
export const MyCheckManageService = (curRes, tabSwitchType) => {
  /** 获取客户列表数据 */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      let requestURL = `${baseUrl}/taskPushController/dataGrid`;
      let requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        order: 'desc',
        ...opt,
        // dealState: opt?.taskType, // taskType  代办flase 已办true
        sort: 'receiveDateTime',
        receiver: getUserID(),

        // 新参数
        sysCode: 'ZYKYPT-SXSQ-007', //固定值
        isRemark: opt?.dealState ? 2 : 0, //0=待办 2=已办
      };

      delete requestBody['taskType'];
      delete requestBody['total'];

      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getCheckManageRun = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    if (otherRequest?.createDateTime) {
      const [createTimeStart, createTimeEnd] = otherRequest.createDateTime;
      otherRequest['createDateTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      otherRequest['createDateTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete otherRequest['createDateTime'];
    }

    if (otherRequest?.endTime) {
      const [endTimeStart, endTimeEnd] = otherRequest.endTime;
      otherRequest['endTimeStart'] = endTimeStart.format('YYYY-MM-DD 00:00:00');
      otherRequest['endTimeEnd'] = endTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete otherRequest['endTime'];
    }

    delete otherRequest['assigneeFormNameDept'];
    delete otherRequest['assigneeFormNameUser'];
    if (otherRequest?.assigneeID && otherRequest?.assigneeID?.length > 0) {
      otherRequest['assigneeID'] = otherRequest?.assigneeID?.toString();
    } else {
      delete otherRequest['assigneeID'];
    }

    run({ ...otherRequest });
  };

  /** 页面列表 */
  let returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /** 获取权限参数 */
  const queryParams: ManageCommonAPI.ISwitchList = ManageTabSwitch(curRes);

  /** 搜索列表 */
  let searchList = React.useMemo(() => {
    return {
      searchList: getSearchList(queryParams, tabSwitchType),
    };
  }, [tabSwitchType]);

  return {
    homePageLoading: loading,
    getCheckManageRun,
    reloadTaskData: refresh,
    switchList: queryParams,
    ...searchList,
    ...returnTableProps,
  };
};

/**
 * 管理页面处理数据
 * @param curRes  权限参数
 *
 * 【禁用】
 */
const handlCheckManageService = (curRes: Record<string, any>, tabSwitchType: string) => {
  /** 获取权限参数 */
  const queryParams: ManageCommonAPI.ISwitchList = ManageTabSwitch(curRes);

  /** 获取客户列表数据 */
  const { tableProps, data, loading, run } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const urlTask: string = opt?.dealState ? '/contractController/hisTaskDataGrid' : '/contractController/taskDataGrid/0';

      let requestURL = `${baseUrl}${urlTask}/${opt?.queryType}`;
      let requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        order: 'desc',
        // dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      const defaultSearchClounm = taskSearchParams();

      const requestParams = handleCheckSearchParams(defaultSearchClounm, requestBody, 'contract');

      return requestFunComponFiex('POST', requestURL, requestParams);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getTaskCheckData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    if (request?.createTime) {
      const [createTimeStart, createTimeEnd] = request.createTime;
      request['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      request['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['createTime'];
    }

    if (request?.endTime) {
      const [endTimeStart, endTimeEnd] = request.endTime;
      request['startTime'] = endTimeStart.format('YYYY-MM-DD 00:00:00');
      request['endTime'] = endTimeEnd.format('YYYY-MM-DD 23:59:59');
      // delete request['endTime'];
    }

    // delete request['assigneeFormNameDept'];
    // delete request['assigneeFormNameUser'];
    // if (request?.assigneeID && request?.assigneeID?.length > 0) {
    //   request['assigneeID'] = request?.assigneeID?.toString();
    // } else {
    //   delete request['assigneeID'];
    // }

    run({
      ...request,
    });
  };

  /** 页面列表 */
  let returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /** 搜索列表 */
  let searchList = React.useMemo(() => {
    return {
      searchList: getSearchList(queryParams, tabSwitchType),
    };
  }, [tabSwitchType]);

  return {
    loading,
    getTaskCheckData,
    switchList: queryParams,
    ...returnTableProps,
    ...searchList,
  };
};

export { handlCheckManageService };
